# WebSocket 配置检查脚本

Write-Host "WebSocket 配置检查工具" -ForegroundColor Cyan
Write-Host "======================" -ForegroundColor Cyan
Write-Host ""

$rootDir = "D:\HubGit\redfire"
$envFile = Join-Path $rootDir "frontend\.env"

# 1. 检查 .env 文件
Write-Host "[1] 检查 .env 文件..." -ForegroundColor Yellow

if (Test-Path $envFile) {
    Write-Host "  OK - .env 文件存在" -ForegroundColor Green
    $content = Get-Content $envFile
    $wsLine = $content | Where-Object { $_ -match "VITE_WS_URL" }
    
    if ($wsLine) {
        Write-Host "  当前配置: $wsLine" -ForegroundColor White
        
        if ($wsLine -match "8004") {
            Write-Host "  OK - 端口配置正确 (8004)" -ForegroundColor Green
        } else {
            Write-Host "  ERROR - 端口配置错误" -ForegroundColor Red
        }
    } else {
        Write-Host "  ERROR - 未找到 VITE_WS_URL" -ForegroundColor Red
    }
} else {
    Write-Host "  ERROR - .env 文件不存在" -ForegroundColor Red
}

Write-Host ""

# 2. 检查后端服务
Write-Host "[2] 检查后端服务..." -ForegroundColor Yellow

try {
    $response = Invoke-RestMethod -Uri "http://localhost:8004/api/v1/market/health" -TimeoutSec 3
    Write-Host "  OK - 服务运行中 (端口 8004)" -ForegroundColor Green
} catch {
    Write-Host "  ERROR - 服务未运行" -ForegroundColor Red
    Write-Host "  请运行: python start_redfire_dev.py" -ForegroundColor Yellow
}

Write-Host ""

# 3. 检查代码配置
Write-Host "[3] 检查代码配置..." -ForegroundColor Yellow

$hookFile = Join-Path $rootDir "frontend\packages\api-client\src\hooks\useSmartMarketData.ts"

if (Test-Path $hookFile) {
    $code = Get-Content $hookFile -Raw
    
    if ($code -match "ws://localhost:8001") {
        Write-Host "  WARNING - 代码中仍使用 8001 端口" -ForegroundColor Yellow
        Write-Host "  需要修改 useSmartMarketData.ts" -ForegroundColor Yellow
    } else {
        Write-Host "  OK - 代码配置正确" -ForegroundColor Green
    }
}

Write-Host ""
Write-Host "======================" -ForegroundColor Cyan
Write-Host "检查完成" -ForegroundColor Cyan
Write-Host ""
Write-Host "下一步操作:" -ForegroundColor White
Write-Host "1. 重启前端服务: cd frontend && npm run dev" -ForegroundColor Gray
Write-Host "2. 打开浏览器开发者工具查看 WebSocket 连接" -ForegroundColor Gray
Write-Host ""

